//
//  XQLoginProtocolView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQLoginProtocolView: UIView {
    
    var protocolClickHandle: (() -> Void)?
    
    public lazy var radioBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "login_unradio"), for: .normal)
        btn.setImage(UIImage(named: "login_radio"), for: .selected)
        btn.isSelected = true
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        btn.layer.masksToBounds = true
        return btn
    }()

    private lazy var prefixLb : UILabel = {
        let lb = UILabel()
        lb.textColor = UIColor(hex: "#A3A6A9")
        lb.font = UIFont.systemFont(ofSize: 13)
        return lb
    }()
    
    private lazy var suffixBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.mainTextColor, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.addTarget(self, action: #selector(protocolClick), for: .touchUpInside)
        return btn
    }()
    
    var prefixStr : String = "" {
        didSet {
            self.prefixLb.text = prefixStr
        }
    }
    
    var suffixStr : String = "" {
        didSet {
            self.suffixBtn.setTitle(suffixStr, for: .normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.radioBtn)
        self.addSubview(self.prefixLb)
        self.addSubview(self.suffixBtn)
    }
    
    private func makeConstraints() {
        self.radioBtn.snp.makeConstraints { make in
            make.top.left.bottom.equalToSuperview()
            make.width.height.equalTo(20)
        }
        self.prefixLb.snp.makeConstraints { make in
            make.left.equalTo(self.radioBtn.snp.right).offset(6)
            make.centerY.equalTo(self.radioBtn)
        }
        self.suffixBtn.snp.makeConstraints { make in
            make.centerY.equalTo(self.radioBtn)
            make.left.equalTo(self.prefixLb.snp.right)
        }
    }
    
    @objc func btnClick() {
        self.radioBtn.isSelected = !self.radioBtn.isSelected
    }
    
    @objc func protocolClick() {
        protocolClickHandle?()
    }
}
